package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.CodeChangeLog;
import com.tbit.uqbike.service.business.CodeChangeLogService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.log.CodeChangeLogDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: chen
 * @Description: 二码绑定日志
 * @Date: 创建于 16:48 2020/8/28
 */
@Service
public class CodeChangeLogServiceImpl implements CodeChangeLogService {
    @Autowired
    private CodeChangeLogDao codeChangeLogDao;
    @Resource
    private AccountDao accountDao;

    @Override
    public List<CodeChangeLog> queryPage(String accountIds, String key, Long startTime, Long endTime,
                                         Integer pageNO, Integer rowCount, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("key", key);
        if (startTime != null) {
            map.put("startTime", new Date(startTime));
        }
        if (endTime != null) {
            map.put("endTime", new Date(endTime));
        }
        List<Integer> accountIdList = getIntegers(accountIds, type);
        if (CollectionUtil.isEmpty(accountIdList)){
            throw new BaseException("请先选择运营区，在进行日志查询");
        }
        List<Account> accountList = accountDao.getByAccountIds(accountIdList);
        Map<Integer, String> accountMap = accountList.stream().collect(Collectors.toMap(Account::getAccountId, Account::getName));

        map.put("accountIdList", accountIdList);
        map.put("start", (pageNO - 1) * rowCount);
        map.put("rowCount", rowCount);
        List<CodeChangeLog> codeChangeLogs = codeChangeLogDao.queryPage(map);
        for (CodeChangeLog codeChangeLog : codeChangeLogs) {
            codeChangeLog.setAccountName(accountMap.get(codeChangeLog.getAccountId()));
        }
        return codeChangeLogs;
    }

    /**
     * 获取区域id
     * @param accountIds
     * @param type
     * @return
     */
    private List<Integer> getIntegers(String accountIds, Integer type) {
        List<Integer> accountIdList = null;
        if (StrUtil.isNotEmpty(accountIds) && Objects.nonNull(type)) {
            List<String> accountId = Arrays.asList(accountIds.split(","));
            if (1 == type && accountId.size() == 1) {
                accountIdList = accountDao.getAccountIdByBrandId(accountId);
            } else if (2 == type) {
                accountIdList = accountDao.getAccountIdByFranchiseeId(accountId);
            } else if (3 == type) {
                accountIdList = accountId.stream().map(Integer::parseInt).collect(Collectors.toList());
            }
        }
        return accountIdList;
    }

    @Override
    public Integer queryCount(String accountIds, String key, Long startTime, Long endTime, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("key", key);
        if (startTime != null) {
            map.put("startTime", new Date(startTime));
        }
        if (endTime != null) {
            map.put("endTime", new Date(endTime));
        }
        map.put("accountIdList", getIntegers(accountIds, type));
        return codeChangeLogDao.queryCount(map);
    }

    @Override
    public void insert(CodeChangeLog codeChangeLog) {
        codeChangeLog.setChangeTime(new Date());
        codeChangeLogDao.insert(codeChangeLog);
    }
}
